package com.examstack.common.util;

import java.io.*;
import java.util.Properties;

public class PropertyUtil {
    private volatile static Properties mailProp;
    private static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * 加载邮件properties。
     * 如果已存在一个关于邮件的Properties实例，则直接返回已存在的实例，否则创建一个关于邮件的Properties实例
     * @param propPath 文件路径
     * @return
     */
    public static synchronized Properties getMailProp(String propPath) {
        if (mailProp == null) {
            synchronized (PropertyUtil.class) {
                if (mailProp == null) {
                    mailProp = initProp(propPath);
                }
            }
        }
        return mailProp;
    }

    /**
     * 重新加载一个properties文件
     * @param propPath 文件路径
     * @return
     */
    public static Properties initNewProp(String propPath) {
        return initProp(propPath);
    }

    /**
     * 加载一个Properties
     *
     * @param propPath
     * @return
     */
    private static Properties initProp(String propPath) {
        Properties prop = new Properties();
        try {
            InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propPath);
            prop.load(new InputStreamReader(inStream, DEFAULT_ENCODING));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return prop;
        }
    }

}
